<template>
  <div class="pageContainer">
    <h3 style="margin-bottom: 16px;">标签库</h3>
    <div style="display: flex;justify-content: space-between;flex-wrap: wrap;">
      <div style="margin-bottom: 10px;display: flex;flex-wrap: wrap">
        <div v-for="item in kindList" class="tagTypeTitle" :style="{ marginRight: isMobile ? '2px' : '24px'}"
             :key="item.index" @click="handleSetKind(item)"
             :class="kind == item.value ? 'activeKind' : ''">{{ item.label }}
        </div>
      </div>
      <el-input class="searchInput" placeholder="搜素" v-model="content" @change="getTagList"
                style="margin-right: 2px;">
        <img slot="prefix" src="@/assets/svg/search.svg" class="el-input__icon el-icon-search"/>
      </el-input>
    </div>
    <div class="tagArea" style="margin-top:16px;">
      <div class="tagItem">
        <div class="addTitle" style="margin-bottom: 24px">新建标签</div>
        <el-input v-model="name" class="tagInput" placeholder="请输入标签名称" style="margin-bottom: 18px;"
                  :maxlength="10"></el-input>
        <el-select v-model="createKind" class="tagInput" style="width: 100%;" placeholder="请选择标签类型">
          <el-option
              v-for="item in selectKindList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
        <div>
          <div class="smallTextLightBtn" style="margin-top:24px;margin-left: auto;" @click="handleCreate">确认</div>
        </div>
      </div>
      <div class="tagItem" v-for="(item, index) in tagList" :key="index">
        <el-input v-if="item.edit" v-model="item.name" :maxlength="10" style="margin-bottom: 10px"></el-input>
        <div v-else style="display: flex;justify-content: space-between;align-items: center;margin-bottom: 10px;">
          <div style="display: flex;align-items: center;">
            <img src="@/assets/svg/changjing.svg" class="tagIcon" v-if="item.kind ===1"/>
            <img src="@/assets/svg/shijian.svg" class="tagIcon" v-if="item.kind ===2"/>
            <img src="@/assets/svg/ziyou.svg" class="tagIcon" v-if="item.kind ===3"/>
            <div class="addTitle" style="margin-left:8px;">{{ item.name }}</div>
          </div>
          <div style="width: 72px;text-align: center;height: 24px;box-sizing: border-box;margin-left: 16px;">
            <div v-if="item.status == 1"
                 style="background: #E6FCE6;color: #46CE48;padding:6px 10px;display: flex;align-items: center">
              <img src="@/assets/svg/using.svg" style="width: 12px;height: 12px"/>
              <div style="margin-left: 3px;font-size: 12px;line-height: 1">使用中</div>
            </div>
            <div v-else style="background: #FFF1EF;color: #FF5133;padding:6px 10px;display: flex;align-items: center;">
              <img src="@/assets/svg/stopUsing.svg" style="width: 12px;height: 12px"/>
              <div style="margin-left: 3px;font-size: 12px;line-height: 1">已停用</div>
            </div>
          </div>
        </div>
        <div class="tagDate defaultTextColor" >{{ item.createDate }}</div>
        <div class="tagMid" :style="{ marginBottom: item.edit ? '10px' : '35px'}">
          <div class="tagCountArea" >
            <div style="font-weight: bold">{{ item.photoCount }}</div>
            <div style="color: #666666;margin-top:14px;">摄影师</div>
          </div>
          <div
              class="tagCountArea">
            <div style="font-weight: bold">{{ item.channelCount }}</div>
            <div style="color: #666666;margin-top:14px;">渠道</div>
          </div>
          <div
              class="tagCountArea">
            <div style="font-weight: bold">{{ item.userCount }}</div>
            <div style="color: #666666;margin-top:14px;">用户</div>
          </div>
        </div>
        <div v-if="item.edit">
          <div class="smallTextLightBtn" style="margin-left: auto;" @click="handleEdit(item)">保存</div>
        </div>
        <div v-else style="display: flex;justify-content: flex-end;align-items: center">
          <div style="display: flex;align-items: center">
            <div style="font-size: 14px;color: #666666;display: flex;align-items: center;">
              <img src="@/assets/svg/edit.svg" @click="setEditMode(index)" class="modifyBtn"/>
              <div style="cursor: pointer" class="defaultTextColor" @click="setEditMode(index)">编辑</div>
            </div>
            <div style="font-size: 14px;color: #666666;display: flex;align-items: center;margin-left: 24px;">
              <el-popconfirm
                  title="确定要删除吗？"
                  @onConfirm="setDelete(item)"
              >
                <div slot="reference" style="display: flex;align-items: center">
                  <img src="@/assets/svg/delete.svg" class="modifyBtn" style="margin-bottom: -1px"/>
                  <div style="cursor: pointer" class="defaultTextColor">删除</div>
                </div>
              </el-popconfirm>

            </div>
          </div>
          <div style="margin-left: 24px;">
              <span v-if="item.status == 1">
          <el-popconfirm
              title="确定要停止使用吗？"
              @onConfirm="setDownStatus(item, 0)"
          >
            <div slot="reference" style="border: 1px solid var(--warningColor);color: var(--warningColor);" class="defaultBtn">停止使用</div>
          </el-popconfirm>
          </span>
            <span v-else>
            <el-popconfirm
                title="确定要恢复使用吗？"
                @onConfirm="setDownStatus(item,1)"
            >
            <div slot="reference" class="defaultBtn">恢复使用</div>
          </el-popconfirm>
          </span>
          </div>

        </div>
      </div>
    </div>
    <el-pagination
        style="margin-left: auto;margin-top:20px;"
        @current-change="handleSizeChange"
        :current-page.sync="pageNum"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
  </div>
</template>
<script>
import {createTag, deleteTag, getTagList, setStatus, updateTag} from "@/api/tag";
import {getThrottle} from "@/utils/utils";

export default {
  name: 'Tag',
  data() {
    return {
      kindList: [
        {
          label: '全部',
          value: null,
        },
        {
          label: '场景标签',
          value: 1,
        },
        {
          label: '事件标签',
          value: 2,
        },
        {
          label: '自由标签',
          value: 3,
        },
      ],
      selectKindList: [
        {
          label: '场景标签',
          value: 1,
        },
        {
          label: '事件标签',
          value: 2,
        },
        {
          label: '自由标签',
          value: 3,
        },
      ],
      kind: null,
      createKind: null,
      name: '',
      content: '',
      pageSize: 11,
      pageNum: 1,
      tagList: [],
      total: 0,

    }
  },
  computed: {
    isMobile() {
      const userAgent = navigator.userAgent || navigator.vendor || window.opera;
      return /Mobi|Android/i.test(userAgent);
    }
  },
  created() {
    this.getTagList(true)

  },
  mounted() {
    this.handleEdit = getThrottle(this.handleEdit, 1000)
    this.handleCreate = getThrottle(this.handleCreate, 1000)
  },
  methods: {
    handleSizeChange(e) {
      this.pageNum = e
      this.getTagList()
    },
    setDelete(e) {
      deleteTag({id: e.id}).then(res => {
        if (res) {
          this.$message.success('操作成功')
          this.getTagList()
        }
      })
    },
    handleEdit(e) {
      if (!e.name) return this.$message.error('请输入名称')
      const requestParam = {
        name: e.name,
        id: e.id
      }
      updateTag(requestParam).then((res) => {
        if (res) {
          this.$message.success('修改成功')
          this.getTagList()
        }
      })
    },
    setEditMode(index) {
      const item = this.tagList[index]
      item.edit = true
      this.tagList.splice(index, 1, item)

    },
    setDownStatus(e, status) {
      const requestParam = {
        id: e.id,
        status,
      }
      setStatus(requestParam).then(res => {
        if (res) {
          this.$message.success('操作成功')
          this.getTagList()
        }
      })
    },
    handleSetKind(e) {
      this.kind = e.value
      this.getTagList()
    },
    resetForm() {
      this.name = null;
      this.createKind = null;
    },
    handleCreate() {
      if (!this.name || !this.createKind) {
        return this.$message.error('请完成所有信息后提交')
      }
      const requestParam = {
        name: this.name,
        kind: this.createKind
      }
      createTag(requestParam).then(res => {
        if (res) {
          this.$message.success('成功！')
          this.resetForm()
          this.getTagList(true)
        }
      })
    },
    getTagList(init = false) {
      if (init) {
        this.pageNum = 1
      }
      const requestParam = {
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        param: {
          kind: this.kind,
          content: this.content
        }
      }
      getTagList(requestParam).then(res => {
        if (res) {
          this.tagList = res.list
          this.total = res.total
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.pageContainer {
  padding: 10px 30px;
  padding-top: 32px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: flex-start;
  //padding-right: 140px;
  //width: 75%;
  width: 1410px;
}

.activeKind {
  background: #EDF2FA !important;
  color: var(--basicColor) !important;
}

.tagItem {
  padding: 24px;
  width: 336px;
  height: 232px;
  box-sizing: border-box;
  border-radius: 4px;
  border: 1px solid #F2F2F2;
  background: #FFFFFF;

  .addTitle {
    font-weight: 600;
    font-size: 16px;
    color: #333333;
    font-style: normal;
  }

  .tagInput {
    background: #FFFFFF;
    height: 32px;
  }
}

.confirmBtn {
  width: 56px;
  height: 24px;
  line-height: 24px;
  background: #2A7EFF;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #FFFFFF;
  float: right;
  text-align: center;
  cursor: pointer;
  user-select: none;
}

.tagIcon {
  width: 24px;
  height: 24px;
  vertical-align: middle;
}

.tagArea {
  display: grid;
  grid-template-columns: repeat(4, auto);
  justify-content: start;
  grid-gap: 16px;
  row-gap: 16px;
}

.icon {
  width: 40px;
  height: 40px;
}

.searchInput {
  width: 280px;
  height: 40px;
  background: #F7F7F7;
  border-radius: 20px;
  //border: 1px solid #DADADA;
}

.searchInput ::v-deep .el-input__inner {
  width: 280px;
  height: 40px;
  background: #F7F7F7;
  border-radius: 20px;
  padding-left: 56px;
  border: 1px solid #DADADA;
}

.searchInput ::v-deep .el-input__prefix {
  padding-left: 16px;
}

.defaultBtn {
  width: 64px;
  height: 24px;
  line-height: 24px;
  border-radius: 2px;
  border: 1px solid var(--basicColor);
  font-weight: 400;
  font-size: 12px;
  color: var(--basicColor);
  text-align: center;
  cursor: pointer;
  user-select: none;
}

.modifyBtn {
  width: 24px;
  height: 24px;
  cursor: pointer;
  user-select: none;
}

.tagDate {
  font-weight: 400;
  font-size: 12px;
  color: var(--defaultColor)
}

.tagMid {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 500;
  margin-top: 26px;

}

.tagCountArea {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  color: #333333;
  padding: 0 27px;
}

.tagTypeTitle {
  width: 80px;
  height: 32px;
  line-height: 32px;
  border-radius: 20px;
  font-weight: 400;
  font-size: 12px;
  color: #666666;
  text-align: center;
  cursor: pointer;
  user-select: none;
}

@media (max-width: 768px) {
  .pageContainer {
    padding-right: 30px;
    width: 100%;
  }
  .tagArea {
    grid-template-columns: repeat(1, auto);
  }
  //.tagItem {
  //  flex: 0 1 100%; /* 在非常小的屏幕上，每个元素占满整行 */
  //}
}

</style>

